package com.mocha.bss.domain.general.contoller;


import com.coffee.core.CoffeeResponse;
import com.coffee.core.CoffeeResponseEnum;
import com.coffee.security.annotation.CoffeePermission;
import com.mocha.bss.config.BssResponseEnum;
import com.mocha.bss.domain.general.contoller.vo.AppVersionVO;
import com.pinghua.mocha.basics.setting.RedisKeyConstant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController
@RequestMapping("/sys")
@AllArgsConstructor
public class SysController {

    private final RedisTemplate<String, Object> redisTemplate;
    private final String APP_KEY = "app_reg";

    /** 监管APP 版本号 */
    @GetMapping("/app/v")
    @CoffeePermission("test_xx")
    public CoffeeResponse<AppVersionVO> getAppVersion(@RequestParam("vno") String vno) {
        try {
            log.info("---------------获取版本信息 | vno:{}", vno);
            AppVersionVO appVersionVO = (AppVersionVO) redisTemplate.opsForHash().get(RedisKeyConstant.SYS_VERSION_MAP, APP_KEY);
            if (null == appVersionVO){
                return CoffeeResponse.response(CoffeeResponseEnum.NotData);
            }
            if (vno.equals(appVersionVO.getVno())){
                return CoffeeResponse.response(BssResponseEnum.VersionSame);
            }

            return CoffeeResponse.responseSuccess(appVersionVO);
        } catch (Exception e) {
            log.error("获取版本信息失败 | vno:{} | {}", vno, e.getMessage());
            throw new RuntimeException("获取版本信息失败");
        }
    }
    /** 监管APP 版本号 */
    @PostMapping("/app/v/add")
    public CoffeeResponse<?> addAppVersion(@RequestBody AppVersionVO appVersionVO) {
        try {
            redisTemplate.opsForHash().put(RedisKeyConstant.SYS_VERSION_MAP, APP_KEY, appVersionVO);
            return CoffeeResponse.success();
        } catch (Exception e) {
            log.error("获取版本信息失败 | vno:{} | {}", appVersionVO, e.getMessage());
            throw new RuntimeException("获取版本信息失败");
        }
    }


}
